﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace Songhay.Silverlight.BiggestBox.IndexPart.ViewModels
{
    using Extensions;
    using Songhay.Models;
    using Songhay.Mvvm.Models;
    using Songhay.Mvvm.ViewModels;
    using Songhay.Mvvm.Views;

    /// <summary>
    /// Models the User Control samples for targetValues.
    /// </summary>
    public class UserControlsViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance
        /// of the <see cref="UserControlsViewModel"/> class.
        /// </summary>
        public UserControlsViewModel()
        {
            if(this.IsInDesignMode) return;

            #region commanding:

            this.LoadUserControlCommand = new RelayCommand<string>(
                    param =>
                    {
                        var location = string.Format("/sample/usercontrol/{0}", param);
                        var message = new LightNavigationMessage<string>(location, "LoadUserControlCommand");
                        Messenger.Default.Send(message);
                    });

            #endregion

            this.Controls = new ObservableCollection<LazyDisplayItem<UserControl>>();

            #region messaging:

            Messenger.Default.Register<LightNavigationMessage<NavigationEventArgs>>(this,
            message =>
            {
                if(message.Source != LightMessageSource.ViewNavigatedTo) return;
                if(message.UniqueId != "UserControlsPage") return;

                var notificationAction = new LightNotificationMessageAction<string>(message.UniqueId,
                pageParameter =>
                {
                    if(string.IsNullOrEmpty(pageParameter)) return;

                    var control = this.Controls
                        .Where(uc => uc.Name == pageParameter)
                        .FirstOrDefault();

                    //TODO: consider using the Error child window.
                    if(control == null) throw new Exception("User Control not found.");

                    var alert = new AlertChildWindow(control.LazyItem.Value);
                    alert.Show();
                })
                {
                    Source = LightMessageSource.ViewModel,
                    UniqueId = "SamplesList"
                };
                Messenger.Default.Send(notificationAction);
            });

            Messenger.Default.Register<LightMessage<IEnumerable<LazyDisplayItem<UserControl>>>>(this,
            message =>
            {
                if(message.Source != LightMessageSource.CompositionMessagingCore) return;
                if(message.UniqueId != "UserControlsPage") return;

                message.Content.ForEachInEnumerable(i => this.Controls.Add(i));
                this.Controls = new ObservableCollection<LazyDisplayItem<UserControl>>(this.Controls.OrderBy(i => i.DisplayName));
            });

            #endregion
        }

        /// <summary>
        /// Gets the load-user-control command.
        /// </summary>
        /// <value>The load-user-control command.</value>
        public ICommand LoadUserControlCommand { get; private set; }

        /// <summary>
        /// Gets or sets the collection of sample user controls.
        /// </summary>
        /// <value>The controls.</value>
        public ObservableCollection<LazyDisplayItem<UserControl>> Controls
        {
            get { return this._controls; }
            set
            {
                this._controls = value;
                base.RaisePropertyChanged("Controls");
            }
        }

        ObservableCollection<LazyDisplayItem<UserControl>> _controls;
    }
}
